package cn.yerly.sakura.web.controller.api;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.yerly.sakura.core.entity.ArcResponse;
import cn.yerly.sakura.dao.model.Product;
import cn.yerly.sakura.service.ProductService;

/**
 * ProductAPI : 商品数据api
 */
@RestController
@RequestMapping("/product")
public class ProductAPI {

	private final Logger logger = LoggerFactory.getLogger(ProductAPI.class);

	@Resource
	private ProductService productService;

	// ////////////////////////////////////////
	// 商品api数据接口
	// ////////////////////////////////////////

	/**
	 * 新增商品
	 * 
	 * @param product
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public ArcResponse<Product> addProduct(@RequestBody Product product) {

		productService.addModel(product);

		// 包装返回的数据
		return new ArcResponse<Product>(HttpStatus.CREATED.value(), "创建成功",
				product);
	}

	/**
	 * 获取单个商品详情
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public ArcResponse<Product> product(@PathVariable("id") Long id) {

		Product product = productService.getModelById(id);

		return new ArcResponse<Product>(HttpStatus.OK.value(), "查询成功", product);
	}

	/**
	 * 编辑产品
	 * 
	 * @param id
	 * @param product
	 * @return
	 */
	@RequestMapping(method = RequestMethod.PUT)
	@ResponseBody
	public ArcResponse<Product> modifyProduct(@PathVariable("id") Long id,
			@RequestBody Product product) {

		product.setId(id);
		Product result = productService.modifyModelById(product);

		return new ArcResponse<Product>(HttpStatus.OK.value(), "修改成功", result);
	}

	/**
	 * 删除商品
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(method = RequestMethod.DELETE)
	@ResponseBody
	public ArcResponse<Product> removeProduct(@PathVariable("id") Long id) {

		productService.removeModelById(id);

		ArcResponse<Product> arc = new ArcResponse<Product>(
				HttpStatus.NO_CONTENT.value(), "删除成功");

		return arc;
	}

	/**
	 * 获取所有商品
	 * 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public ArcResponse<Iterable<Product>> products() {

		Iterable<Product> products = productService.getModels();
		
		ArcResponse<Iterable<Product>> arc = new ArcResponse<Iterable<Product>>(
				HttpStatus.OK.value(), "获取全部商品成功", products);

		return arc;
	}

	// ////////////////////////////////////////
	// 用户商品关系api数据接口
	// ////////////////////////////////////////

	/**
	 * 收藏商品, 返回收藏列表
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}/star", method = RequestMethod.PUT)
	@ResponseBody
	public ArcResponse<List> star(@PathVariable("id") Long id) {

		List products = productService.starProduct(id);

		ArcResponse<List> arc = new ArcResponse<List>(HttpStatus.OK.value(),
				"收藏商品[" + id + "]成功", products);

		return arc;
	}

	/**
	 * 删除收藏商品, 返回收藏列表
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}/unStar", method = RequestMethod.DELETE)
	@ResponseBody
	public ArcResponse<List> unStar(@PathVariable("id") Long id) {

		List products = productService.unStarProduct(id);

		ArcResponse<List> arc = new ArcResponse<List>(HttpStatus.OK.value(),
				"删除收藏商品[" + id + "]成功", products);

		return arc;
	}
}
